#include "iw_additem.h"
#include "ui_iw_additem.h"

IW_AddItem::IW_AddItem(QWidget *parent,
                       const QString &table_,
                       const QString &category_) :
    QDialog(parent),
    ui(new Ui::IW_AddItem)
{
    ui->setupUi(this);

    table=table_;
    category=category_;
}

IW_AddItem::~IW_AddItem()
{
    delete ui;
}

bool IW_AddItem::addItem(const QString &detail,
                         const QString &addition)
{

    if(category.isEmpty())
        return false;

    QSqlQuery query;
    query.prepare(QString("insert into %1 (month_,date_,category,detail,addition) values (?,?,?,?,?)")
                  .arg(table));
    query.addBindValue(CurrentMonth);
    query.addBindValue(Today);
    query.addBindValue(category);
    query.addBindValue(detail);
    query.addBindValue(addition);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void IW_AddItem::on_btn_submit_clicked()
{
    QString detail=ui->detail->toPlainText().trimmed();
    if(detail.isEmpty())
    {
        MESSAGE_DETAIL_EMPTY
    }

    QString remark=ui->remark->text().trimmed();
    if(addItem(detail,remark))
        accept();
    else
        MESSAGE_CANNOT_SUBMIT
}
